/*
编写 : Hexh
时间 : 2012-5-22 10:43:18
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsIntlVehicle
	/// </summary>
	public class FsIntlVehicleController : BaseController
	{

		private readonly IFsIntlVehicleFacade FsIntlVehicleFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public FsIntlVehicleController(IFsIntlVehicleFacade FsIntlVehicleFacade, IHospUserFacade HospUserFacade)
		{
			this.FsIntlVehicleFacade = FsIntlVehicleFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问FsIntlVehicle管理页面")]
		[ViewPage]
		public ActionResult FsIntlVehicle()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改FsIntlVehicle信息")]
		public ActionResult SaveOrUpdate()
		{
			FsIntlVehicle entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsIntlVehicle
				{
					// ForeignKey Set
					ICCardUID = Request["ICCardUID"],
					ICCard = new InfraIdcard{
						Type = (InfraIdcardType)Convert.ToInt32(Request["Type"]),
						CardUid = Request["ICCardUID"],
						DeptId = Request["Departmentid"],
					},
					Type = (InfraIdcardType)Convert.ToInt32(Request["Type"]),
					CarNo = Request["CarNo"],
					Owner = Request["Owner"], 
					DepartmentId = Request["Departmentid"], 
					Description = Request["Description"], 
					State = Request["State"],
					ApproveId = Request["ApproveId"]
				};
			}
			else
			{
				// get the entity.
				entity = FsIntlVehicleFacade.Get(Request["Id"]);
				entity.ICCardUID = Request["ICCardUID"];
				entity.ICCard.CardUid = Request["ICCardUID"];
				entity.Type = (InfraIdcardType)Convert.ToInt32(Request["Type"]);
				entity.CarNo = Request["CarNo"]; 
				entity.Owner = Request["Owner"]; 
				entity.DepartmentId = Request["Departmentid"]; 
				entity.Description = Request["Description"]; 
				entity.State = Request["State"];
				entity.ApproveId = Request["ApproveId"];
			}
			
			// 保存信息
			FsIntlVehicleFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除FsIntlVehicle基本资料")]
		public ActionResult Delete()
		{
			FsIntlVehicleFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsIntlVehicle基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsIntlVehicleFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsIntlVehicle清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsIntlVehicleFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			// 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsIntlVehicle> { total = total, data = data },
				new string[] { "Department" });
		}
		
		[Description("获取FsIntlVehicle信息")]
		public ActionResult GetById()
		{
			var entity = FsIntlVehicleFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsIntlVehicle> { data = new FsIntlVehicle[] { entity } }, new string[] { "" }
			   );
		}

		[ExtResult]
		[Description("判断FsIntlVehicle字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsIntlVehicleFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
